#coding=utf-8

import os
import commonHelper

# 模板
tsFileTemp ='''namespace basicData{

    /**%mgrDesc%*/
    export class %confMgr% {
    
        private static _instance: %confMgr% ;
        public static get instance():%confMgr%{
			if(!%confMgr%._instance){
			  %confMgr%._instance = new %confMgr%();
			} 
            return %confMgr%._instance;
        }
        
        private _confDict:{[key: number]: %confClass%} ;

        public data:any ;
%dicList%
        constructor(){ 
            this._confDict = {};
            var conf:any = GameConfData.%confField%;
            this.data = conf.data ;
%setDicList%
        }
%dicFuncs%

        private getConf(index:number):%confClass% {  
            if(!this._confDict[index]){
                this._confDict[index] = new %confClass%(this.data[index]);
            } 
            return  this._confDict[index];   
        }

    } 
    
    /**%confDesc%*/
    export class %confClass% { 
    
        data:Array<any> ;
        constructor(data:Array<any>){ 
            this.data = data; 
        }
%fields%
    }
}
import %confMgr% = basicData.%confMgr%;
import %confClass% = basicData.%confClass%;
'''

# 通过关键字获得方法名
def getFuncName(keys,titleTypes ,colNameDic,returnArr):
    name = "getConfBy"
    if returnArr :
        name = "getConfsBy"
    name2 = ""
    params = ""
    for(i,key) in enumerate(keys):
        name2 += key.capitalize()
        if params != "":
            params += ","
        params += key + ":" + commonHelper.getTsParamType(titleTypes[colNameDic[key]])
    return name + name2 + "("+params+")"

def getPureFuncName(keys,titleTypes ,colNameDic,returnArr):
    name = "getConfBy"
    if returnArr :
        name = "getConfsBy"
    name2 = ""
    params = ""
    for(i,key) in enumerate(keys):
        name2 += key.capitalize()
        if params != "":
            params += ","
        params += key + ":" + commonHelper.getTsParamType(titleTypes[colNameDic[key]])
    return name + name2

def getFuncNameTotal(keys,titleTypes ,colNameDic,returnArr):
    name = "getConfBy"
    if returnArr :
        name = "getConfsBy"
    name2 = ""
    params = ""
    for(i,key) in enumerate(keys):
        name2 += key.capitalize()
        if params != "":
            params += ","
        params += key + ":" + commonHelper.getTsParamType(titleTypes[colNameDic[key]])
    return name + name2 + "("+")"

# 通过关键字获得方法名
def getKey(keys):
    name = "" ;
    for(i,key) in enumerate(keys):
        if name != "":
            name += ' + "_" + '
        name += key
    return name

# 获得配置文件的字段定义
def getConfFieldDef(colNameDic,titleTypes,cnName):
    list = "\n"
    for(i,val) in enumerate(colNameDic):
        if val :
            list += "\t\t/**"+ cnName[i] +"*/\n";
            # list += "\t\t" + val + ":" + commonHelper.getTsParamType(titleTypes[i]) + ";\n"
            list += "\t\tget " + val + "():" + commonHelper.getTsParamType(titleTypes[i]) + "{ return this.data[" + str(i) + "]; }\n"
    return list


# 生成一个ts文件
def genTsFile(dir, confName,colKeys, colKeysArray, titleTypes ,colNameDic,cnName,desc):
    confField = confName #配置文件的字段名
    confMgrName = confName.capitalize() + "ConfMgr" #配置文件管理类名字
    confDicList = ""  #词典字段名
    funcList = "" #获得配置的方法
    setDicList = ""#给词典赋值
    confClass = confName.capitalize() + "Conf"  # 具体的配置对象类型
    confClassFields =  getConfFieldDef(colNameDic,titleTypes,cnName) #配置的各个字段
    for(i,keys) in enumerate(colKeys):#只返回一个配置
        dicName = commonHelper.getDicNameByKeys(keys,False)
        confListName = commonHelper.getConfListNameByKeys(keys, True)
        confDicList += "\t\tprivate _" + dicName + ":any ;\n"
        setDicList += "\t\t\tthis._"+dicName+ " = conf." + dicName + ";\n";

        #添加
        confDicList += "\t\tprivate _" + confListName + ":" + confClass + "[];\n"

        #生成相关的方法
        key = getKey(keys)
        funcList += "\t\tpublic "  + getFuncName(keys,titleTypes ,colNameDic,False) + ":" + confClass + "{ \n";
        funcList += "\t\t\t" +  "return this.getConf(<number>this._" + dicName + "["+ key +  "]);\n"
        funcList += "\n\t\t}\n";
        #获取所有
        #private  _confList: % confClass % [];
        #public getConfList(): % confClass % {
        #if (!this._confList){
        #for (let id in _dicId){
        #this._confList.push(this.getConf(index));
        #}
        #}
        #return this._confList;
        #}
        funcName = getPureFuncName(keys, titleTypes, colNameDic, False);

        funcList += "\t\tpublic " + getFuncNameTotal(keys, titleTypes, colNameDic, True) + ":Array<" + confClass + ">{ \n";
        funcList += "\t\t\t" + "if(!this._" + confListName + "){" + "\n"
        #funcList += "\t\t\t\t" + "this._" + confListName + " = new Array()[" + "Object.keys(this._" + dicName + ").length]" +  ";\n"
        funcList += "\t\t\t\t" + "this._" + confListName + " = new Array()" + ";\n"
        funcList += "\t\t\t\t" + "for(var id in this._" + dicName + "){" + "\n"
        funcList += "\t\t\t\t\t" + "this._" + confListName + ".push(this.getConf" + "(<number>this._"+ dicName +"[id]));" + "\n"
        funcList += "\t\t\t\t" + "}\n"
        funcList += "\t\t\t" + "}\n"
        funcList += "\t\t\t" + "return this._" + confListName + ";"
        funcList += "\n\t\t}\n";

    for (i, keys) in enumerate(colKeysArray):#返回配置数组
        dicName = commonHelper.getDicNameByKeys(keys, True)
        confDicList += "\t\tprivate _" + dicName + ":any ;\n"
        setDicList += "\t\t\tthis._" + dicName + " = conf." + dicName + ";\n";
        # 生成相关的方法
        key = getKey(keys)
        funcList += "\t\tpublic " + getFuncName(keys, titleTypes, colNameDic,True) + ": Array<"+ confClass + ">{ \n";
        funcList += "\t\t\t" + "var indexs:Array<number> = this._"+ dicName+ "[" + key + "]\n"
        funcList += "\t\t\t" + "if(!indexs) return null;\n"
        funcList += "\t\t\t" + "var arr:Array<"+ confClass + "> = [];\n"
        funcList += "\t\t\t" + "indexs.forEach((index)=>{arr.push(this.getConf(index))});\n"
        funcList += "\t\t\t" + "return arr;"
        funcList += "\n\t\t}\n";

    content = tsFileTemp.replace("%mgrDesc%", desc + "配置数据管理");
    content = content.replace("%confDesc%", desc + "配置数据");
    content  = content.replace("%confMgr%",confMgrName);
    content = content.replace("%dicList%", confDicList);
    content = content.replace("%dicFuncs%", funcList);
    content = content.replace("%confField%", confField);
    content = content.replace("%setDicList%", setDicList);
    content = content.replace("%confClass%", confClass);
    content = content.replace("%fields%", confClassFields);


    outFile = open(dir + "/" + confName.capitalize() + ".ts", 'w+', encoding='utf-8')
    outFile.write(content)
    outFile.close()

